package com.bjpowernode.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.mapper.TActivityMapper;
import com.bjpowernode.model.TActivity;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.service.ActivityService;
import com.bjpowernode.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import jakarta.annotation.Resource;

@Service
public class ActivityServiceImpl implements ActivityService {

	@Resource
	private TActivityMapper activityMapper;

	@Override
	public PageInfo<TActivity> getActivitysByPage(Integer current, ActivityQuery activityQuery) {

		// 1. 设置PagHelper
		PageHelper.startPage(current, Constants.PAGE_SIZE);
		// 2.查询
		List<TActivity> list = activityMapper.selectActivitysByPage(activityQuery);
		// 3.封装
		PageInfo<TActivity> info = new PageInfo<TActivity>(list);
		
		return info;
	}

	@Override
	public int saveActivity(ActivityQuery activityQuery) {
		
		TActivity activity = new TActivity();
		
		BeanUtils.copyProperties(activityQuery, activity);
		
		// 获取登录人id
		Integer createBy = JWTUtils.parseJWTByUserId(activityQuery.getToken());
		activity.setCreateBy(createBy);
		// 创建时间
		activity.setCreateTime(new Date());
		
		return activityMapper.insertSelective(activity);
	}
	

}
